package com.jetinno.syrup2

import android.text.TextUtils
import com.google.gson.Gson
import com.jetinno.bean.MachineRespond
import com.jetinno.utils.LogUtils


class SyrupControlUtils {

    companion object {
        val TAG = "SirupControlUtils"

        private val mGson = Gson()
        private var mSirup: SyrupProtocol

        init {
            mSirup = SyrupProtocol.instance
        }


        ///查询状态
        fun queryStatus(): MachineRespond {

            var respond: MachineRespond? = mSirup.queryStatus()
            if (respond == null) {
                respond = MachineRespond()
            }
            return respond!!
        }

        ///分杯套
        fun actionCupsleeve(): MachineRespond {

            var respond: MachineRespond? = mSirup.actionCupsleeve()
            if (respond == null) {
                respond = MachineRespond()
            }
            return respond!!
        }

        ///清除故障
        fun clearError(): MachineRespond {
            return mSirup.clearError(0)
        }

        ///糖浆电机动作（校准，动作，清洗都是这里）
        fun calibration(list: MutableList<SyrupMotor>): Boolean {
            try {
                LogUtils.d2File(TAG, "校验传入参数${list}")

                val data = mSirup.executeSugar(list, 2)
                return !TextUtils.isEmpty(data.hexResult)
            } catch (e: Exception) {
                e.printStackTrace()
                LogUtils.d2File(TAG, "校准异常${e.message}")

            }
            return false
        }


        /**
         * @param data E3010000100000007F060100008A
         */
        fun parseQuery(data: String): SyrupQueryReceive {

            var receive = SyrupQueryReceive()
            if (data.isNotBlank()) {
                val content = data.substring(8, data.length - 2)
                if (content.length == 38 || content.length == 18) {
                    receive = SyrupQueryReceive(content)
                }
            }
            return receive
        }

    }
}

